package server.console.groups.setting

import com.alee.laf.list.WebList
import com.alee.laf.scroll.WebScrollPane
import com.alee.laf.table.WebTable
import server.MapleItemInformationProvider
import server.MapleSignin
import java.awt.BorderLayout
import java.awt.Component
import java.util.*
import javax.swing.table.AbstractTableModel


class SiginiConfigGroup(owner: ConfigPanel) : AbstractConfigGroup(owner, "每日签到管理") {

    var siginMonth: MapleSignin.SiginMonth? = MapleSignin.getInstance().siginMonth
    val data = arrayOf(
            arrayOf("1", 2000000, "红色药水", 1, 10, false),
            arrayOf("2", 2000001, "蓝色药水", 2, 10, false),
            arrayOf("3", 2000002, "白色药水", 3, 10, false)
    )
    var monthReward = emptyArray<List<*>>()


    override fun getPreview(): Component {
        MapleSignin.getInstance().load()
        val mainPanel = TitleWebPanel("根据左侧的月份来管理每月的签到奖励")
        val month = Vector<Int>()
        month += 1..12
        val monthList = WebList(month)
        monthList.addListSelectionListener { event ->
            if (event.valueIsAdjusting) {
                val valueAt = monthList.getValueAt<Int>(monthList.selectedIndex)
                siginMonth = MapleSignin.getInstance().getSiginMonth(valueAt)
                println(valueAt)
//                data.
//                for (i in 1..28) {
//                    val iteminfo = ArrayList<Any>()
//                    siginMonth?.rewards?.get(i)?.let {
//                        iteminfo.add(it.rank)
//                        iteminfo.add(it.itemId)
//                        iteminfo.add(MapleItemInformationProvider.getInstance().getName(it.itemId))
//                        iteminfo.add(it.quantity)
//                        iteminfo.add(it.expiredate)
//                        iteminfo.add(it.isCash)
//                        return iteminfo
//                    }
//                }
            }
        }

        mainPanel.add(WebScrollPane(monthList), BorderLayout.WEST)


        val table = WebTable(SiginiTableModle())
        val scrollPane = WebScrollPane(table)

        mainPanel.add(scrollPane)

        return mainPanel
    }

//    fun initColumnSizes(table: JTable) {
//        val mode = table.model as SiginiTableModle
//        var column: TableColumn
//        var comp: Component
//        var headerWidth: Int
//        var cellWidth: Int
//
//    }


    inner class SiginiTableModle : AbstractTableModel() {

        private val columnNames = arrayListOf("日期", "道具ID", "道具名称", "数量", "到期时间(分钟)", "是否带点券标志")


        override fun getRowCount(): Int {
            return 28
        }

        override fun getColumnName(column: Int): String {
            return columnNames[column]
        }

        override fun getColumnCount(): Int {
            return columnNames.size
        }

        override fun getValueAt(day: Int, index: Int): Any {
            val sri: MapleSignin.SiginRewardInfo? = siginMonth?.rewards!![day]
            return sri?.let {
                return when (index) {
                    0 -> {
                        sri.rank
                    }
                    1 -> {
                        sri.itemId
                    }
                    2 -> {
                        sri.let { MapleItemInformationProvider.getInstance().getName(sri.itemId) } ?: ""
                    }
                    3 -> {
                        sri.quantity
                    }
                    4 -> {
                        sri.expiredate
                    }
                    5 -> {
                        sri.isCash
                    }
                    else -> {
                    }
                }
            } ?: let {
                return if (index == 1) {
                    index
                } else {
                    ""
                }
            }
        }

        override fun isCellEditable(rowIndex: Int, columnIndex: Int): Boolean {
            return false
        }

        override fun getColumnClass(columnIndex: Int): Class<*> {
            return when (columnIndex) {
                2 -> {
                    String::class.java
                }
                5 -> {
                    Boolean::class.java
                }
                else -> {
                    Int::class.java
                }
            }
        }
    }
}